﻿Imports BUS
Imports DTO

Public Class frmQuanLySach

    Private Sub frmQuanLySach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbLoaiSach.DataSource = TheLoaiSach_BUS.loadTheLoai
        cbbLoaiSach.DisplayMember = "TenLoaiSach"
        cbbLoaiSach.ValueMember = "MaLoaiSach"

        cbbNhaXuatBan.DataSource = NhaXuatBan_BUS.loadNhaXuatBan
        cbbNhaXuatBan.DisplayMember = "TenNXB"
        cbbNhaXuatBan.ValueMember = "MaNXB"

        cbbTinhTrang.DataSource = TinhTrang_BUS.loadTinhTrang
        cbbTinhTrang.DisplayMember = "TenTinhTrang"
        cbbTinhTrang.ValueMember = "MaTinhTrang"

        loadViTri()

        dgvDSSach.DataSource = CuonSach_BUS.loadInfoSach
        binding()
    End Sub

    Sub loadViTri()
        cbbViTri.DataSource = ViTri_BUS.loadViTri
        cbbViTri.DisplayMember = "MaViTri"
        cbbViTri.ValueMember = "MaViTri"
    End Sub

    Dim vitri As Integer = 0

    Sub binding()
        cbbViTri.DataSource = ViTri_BUS.loadViTri
        cbbViTri.DisplayMember = "MaViTri"
        cbbViTri.ValueMember = "MaViTri"
        With dgvDSSach.Rows(vitri)
            lblMaSach.Text = .Cells(0).Value.ToString
            txtTuaSach.Text = .Cells(1).Value.ToString
            txtTacGia.Text = .Cells(2).Value.ToString
            nbSoTrang.Value = .Cells(3).Value
            nbNamSX.Value = .Cells(4).Value
            nbSLSach.Value = .Cells(5).Value
            txtTomTat.Text = .Cells(6).Value.ToString
            txtDuongDan.Text = .Cells(7).Value.ToString
            cbbLoaiSach.Text = .Cells(8).Value.ToString
            txtGiaTien.Text = Single.Parse(.Cells(9).Value)
            cbbNhaXuatBan.Text = .Cells(10).Value.ToString
            cbbTinhTrang.Text = .Cells(11).Value.ToString
            cbbViTri.Text = .Cells(12).Value.ToString
        End With
    End Sub

    Private Sub dgvDSSach_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDSSach.CellClick
        vitri = e.RowIndex
        binding()
    End Sub

    Private Sub btnLamMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamMoi.Click
        lblMaSach.Text = "0"
        txtTuaSach.Text = ""
        txtTacGia.Text = ""
        txtGiaTien.Text = ""
        txtDuongDan.Text = ""
        txtTomTat.Text = ""
        nbSLSach.Value = 0
        nbNamSX.Value = 1900
        nbSoTrang.Value = 0
        cbbLoaiSach.Text = ""
        cbbNhaXuatBan.Text = ""
        cbbViTri.Text = ""
        cbbTinhTrang.Text = ""
    End Sub

    Private Sub btnLay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLay.Click
        cbbViTri.DataSource = Nothing
        cbbViTri.DataSource = ViTri_BUS.loadViTriTrong
        cbbViTri.DisplayMember = "MaViTri"
        cbbViTri.ValueMember = "MaViTri"
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim ds As New DauSach_DTO
        ds.tuaSach = txtTuaSach.Text
        ds.tacGia = txtTacGia.Text
        ds.soTrang = nbSoTrang.Value
        ds.soLuongSach = nbSLSach.Value
        ds.namSanXuat = nbNamSX.Value
        ds.tomTat = txtTomTat.Text
        ds.giaTien = txtGiaTien.Text
        ds.maLoaiSach = cbbLoaiSach.SelectedValue
        ds.hinhAnh = txtDuongDan.Text

        Dim cs As New CuonSach_DTO
        cs.maNXB = cbbNhaXuatBan.SelectedValue
        cs.maTinhTrang = cbbTinhTrang.SelectedValue
        cs.maViTri = cbbViTri.SelectedValue

        CuonSach_BUS.themSach(ds, cs)

        MessageBox.Show("Thêm thành công!")

    End Sub
End Class